﻿using HBase;
using hCommand;
using Persistence;

using System;
using System.Collections.Generic;

namespace Controller
{
    public class WmsBackTask : TaskBackTask
    {

        ICoreStockServer wmsServer = WMSDalFactray.getDal<ICoreStockServer>();
        ICSDServer csdServer = WMSDalFactray.getDal<ICSDServer>();
        IDoServer doServer = WMSDalFactray.getDal<IDoServer>();
        protected override bool onlyOneTimeAsync()
        {
            time = 60000;
            return true;
        }

        public override bool excute()
        {
            /*List<CoreStock> stockList = wmsServer.getBackStock();
            foreach( CoreStock cs in stockList ) {
                backStock(cs);
            }*/

            deleteTasks(wmsServer.getfinshTasksforDelete(48));
            // DateTime date = DateTime.Now;
            // int key = date.Year * 100 + date.Month;
            //statServer.stat(key);

            List<WhDoo> dois = wmsServer.GetAll<WhDoo>(" and status=9 and TIMESTAMPDIFF(hour ,finsh_Time ,now())>120");
            foreach (WhDoo doo in dois)
            {
                backDet(doo);
            }

            List<CoreStock> coreStockList = wmsServer.GetAll<CoreStock>(" and status<0 and TIMESTAMPDIFF(hour ,change_Time ,now())>120");
            foreach (CoreStock cs in coreStockList)
            {
                back(cs);
            }//绑定前删除
            List<CoreStockDet> csdList = wmsServer.GetAll<CoreStockDet>(" and status<0 and pid not in (select id from core_stock)  and doo_Det_Id =0");
            foreach (var csd in csdList) {
                back(csd);
            }
            return true;

        }

        void backDet(WhDoo doo)
        {
            List<WhDooDet> dets = doServer.getDetials<WhDooDet>(doo.id);
            List<CoreStockDet> allCsd = new List<CoreStockDet>();
            foreach (WhDooDet det in dets)
            {
                List<CoreStockDet> csds = doServer.GetAll<CoreStockDet>(" and doo_det_id=@id", det);
                foreach (CoreStockDet csd in csds)
                {
                    if (csd.status > 0)
                    {
                        return;
                    }
                }
                allCsd.AddRange(csds);
            }

            foreach (CoreStockDet det in allCsd)
            {
                back(det);
            }


            doo.status = 10;

            wmsServer.updateNotNull(doo);

        }

        void backStock(CoreStock cs)
        {
            List<CoreStockDet> detials = csdServer.GetListByStockId(cs.id);
            foreach (CoreStockDet d in detials)
            {
                if (d.status != -1)
                {
                    return;
                }
            }
            backTask(cs.id);
            foreach (CoreStockDet d in detials)
            {
                back(d);
            }
            back(cs);
        }





    }
}
